from db.db_handler import MySQLHandler
from lib import common

login_dict_teacher = {'username': '', 'role': ''}


def login(username, password):
    user_info = common.user_exists(username)
    if not user_info:
        return '【用户不存在】'
    else:
        if 'teacher' not in user_info['role']:
            return f'【当前用户 {username} 权限错误！】'
        true_pwd = user_info['password']
        salt = user_info['password'][-4:]
        input_pwd = common.encrypt_data(password, salt)
        if true_pwd != input_pwd:
            return '【用户名或密码有误】'
        else:
            login_dict_teacher['username'] = username
            login_dict_teacher['role'] = 'teacher'
            return '【登录成功】'


def select_course(course_id):
    mysql_obj = MySQLHandler()
    teacher_id = common.tch_username2tch_id(username=login_dict_teacher['username'])['id']
    update_sql = "update course2teacher set course_id=%(course_id)s where teacher_id=%(teacher_id)s"
    res = mysql_obj.update_data(sql=update_sql, course_id=course_id, teacher_id=teacher_id)
    if res == 0:
        return '【修改选课失败,请联系系统管理员】'
    elif res == 1:
        course_name = common.course_id2course_name(course_id=course_id)['course_name']
        return f'【讲师{login_dict_teacher["username"]}修改选课成功，修改后的课程为 {course_name} 】'
    else:
        return f'【修改选课失败失败 {res} 】'


def check_student():
    mysql_obj = MySQLHandler()
    teacher_sql = "select id from teacher where name=%(name)s"
    teacher_id = mysql_obj.search_one(sql=teacher_sql, name=login_dict_teacher['username'])['id']
    course_sql = "select course_id from course2teacher where teacher_id=%(tch_id)s"
    course_id = mysql_obj.search_one(sql=course_sql, tch_id=teacher_id)['course_id']
    course_name = common.course_id2course_name(course_id=course_id)['course_name']
    search_sql = """select student.name,course2student.scores from student 
                    join course2student on student.id=course2student.student_id 
                    join course on course2student.course_id=course.id
                    where course2student.course_id=1"""
    stu_dict = mysql_obj.search_all(sql=search_sql, course_id=course_id)
    print(f"当前课程【{course_name}】".center(35, '='))
    print("学生如下".center(35))
    for stu in stu_dict:
        print(f"学生姓名【{stu['name']}】 学生成绩【{stu['scores']}】".center(35))
    return "查看学生结束".center(35, '=')


def renew_score(choice_stu, new_score: int):
    stu_info = common.stu_username2stu_id(choice_stu)
    if not stu_info:
        return f'学生【{choice_stu}】不存在！'
    stu_id = stu_info['id']
    mysql_obj = MySQLHandler()
    update_sql = "update course2student set scores = %(new_score)s where student_id=%(stu_id)s"
    res = mysql_obj.update_data(sql=update_sql, new_score=new_score, stu_id=stu_id)
    if res == 0:
        return '【修改成绩失败，请联系系统管理员】'
    elif res == 1:
        return f'【修改学生 {choice_stu} 成绩成功，修改后的分数为 {new_score} 】'
    else:
        return f'【发生了意料之外的错误{res}，请联系系统管理员检查】'
